package com.penta.helper;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.penta.util.ExcelHelper;
import org.apache.poi.ss.usermodel.*;

public class CellStyleBuilder {

    private CellStyle cellStyle;
    private ExcelWriter writer;

    public CellStyleBuilder(ExcelWriter writer) {
        this.writer = writer;
        this.cellStyle = writer.getWorkbook().createCellStyle();
    }

    /**
     * 设置字体
     * @param font
     * @return
     */
    public CellStyleBuilder setFont(Font font) {
        this.cellStyle.setFont(font);
        return this;
    }

    /**
     * 是否换行
     *
     * @param b
     *      false: 表示不换行; true: 表示换行。
     * @return
     */
    public CellStyleBuilder setWrapText(boolean b) {
        this.cellStyle.setWrapText(b);
        return this;
    }

    /**
     * 垂直方向
     *
     * @param verticalAlignment
     * @return
     */
    public CellStyleBuilder setVerticalAlignment(VerticalAlignment verticalAlignment) {
        this.cellStyle.setVerticalAlignment(verticalAlignment);
        return this;
    }

    /**
     * 水平方向
     *
     * @param horizontalAlignment
     * @return
     */
    public CellStyleBuilder setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
        this.cellStyle.setAlignment(horizontalAlignment);
        return this;
    }

    /**
     * 设置前景色
     *
     * @param color
     * @return
     */
    public CellStyleBuilder setFillForegroundColor(int color) {
        this.cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        this.cellStyle.setFillForegroundColor((short) color);
        return this;
    }

    /**
     * 设置背景色
     *
     * @param color
     * @return
     */
    public CellStyleBuilder setFillBackgroundColor(int color) {
        this.cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        this.cellStyle.setFillBackgroundColor((short) color);
        return this;
    }

    /**
     * 设置单元格式显示格式
     *
     * @param
     * @return
     */
    public CellStyleBuilder setDataFormat() {

        DataFormat format = writer.getWorkbook().createDataFormat();
        short decimalFormat = format.getFormat("#,##0.00");
        this.cellStyle.setDataFormat(decimalFormat);
        return this;
    }

    /**
     * 设置单元格式显示格式
     *
     * @param
     * @return
     */
    public CellStyleBuilder setDataFormat2() {

        DataFormat format = writer.getWorkbook().createDataFormat();
        short decimalFormat = format.getFormat("$#,##0.00");
        this.cellStyle.setDataFormat(decimalFormat);
        return this;
    }

    /**
     * 设置保留几位数
     *
     * @param
     * @return
     */
    public CellStyleBuilder setDataFormat(int keepBit) {
        StringBuilder sb = new StringBuilder();
        if(keepBit>0) {
            sb.append(".");
            for(int i=1;i<=keepBit;i++) {
                sb.append("0");
            }
        }

        DataFormat format = writer.getWorkbook().createDataFormat();
        short decimalFormat = format.getFormat("0" + sb.toString());
        this.cellStyle.setDataFormat(decimalFormat);
        return this;
    }

    /**
     * 设置边框,边框的颜色为黑色
     *
     * @param borderList
     *          0：表示不设置边框
     *          1234：表示四边都设置边框
     *          12：表示上左设置边框
     * @return
     */
    public CellStyleBuilder setCellBorder(String borderList) {
        /**
         * 先将所有的边都设置为无边框
         */
        this.cellStyle.setBorderTop(BorderStyle.NONE);
        this.cellStyle.setBorderLeft(BorderStyle.NONE);
        this.cellStyle.setBorderBottom(BorderStyle.NONE);
        this.cellStyle.setBorderRight(BorderStyle.NONE);

        if("0".equals(borderList)) {
            return this;
        }

        /**
         * 上边框
         */
        if(StrUtil.containsAny(borderList,"1")) {
            this.cellStyle.setBorderTop(BorderStyle.THIN);
        }

        if(StrUtil.containsAny(borderList,"2")) {
            this.cellStyle.setBorderLeft(BorderStyle.THIN);
        }

        if(StrUtil.containsAny(borderList,"3")) {
            this.cellStyle.setBorderBottom(BorderStyle.THIN);
        }

        if(StrUtil.containsAny(borderList,"4")) {
            this.cellStyle.setBorderRight(BorderStyle.THIN);
        }

        return this;
    }


    /**
     * 设置边框,边框的颜色为黑色
     *
     * @param borderList
     *          0：表示不设置边框
     *          1234：表示四边都设置边框
     *          12：表示上左设置边框
     * @param colorIndex
     *          颜色索引
     * @return
     */
    public CellStyleBuilder setCellBorder(String borderList,int colorIndex) {
        /**
         * 先将所有的边都设置为无边框
         */
        this.cellStyle.setBorderTop(BorderStyle.NONE);
        this.cellStyle.setBorderLeft(BorderStyle.NONE);
        this.cellStyle.setBorderBottom(BorderStyle.NONE);
        this.cellStyle.setBorderRight(BorderStyle.NONE);

        if("0".equals(borderList)) {
            return this;
        }

        /**
         * 上边框
         */
        if(StrUtil.containsAny(borderList,"1")) {
            this.cellStyle.setBorderTop(BorderStyle.THIN);
        }

        if(StrUtil.containsAny(borderList,"2")) {
            this.cellStyle.setBorderLeft(BorderStyle.THIN);
        }

        if(StrUtil.containsAny(borderList,"3")) {
            this.cellStyle.setBorderBottom(BorderStyle.THIN);
        }

        if(StrUtil.containsAny(borderList,"4")) {
            this.cellStyle.setBorderRight(BorderStyle.THIN);
        }

        // 设置前景色
        this.cellStyle.setFillForegroundColor((short)colorIndex);
        return this;
    }


    /**
     * 设置边框，每条边框对应一个颜色
     * 主要是用于设置物料列表的边框颜色
     *
     *
     * @return
     */
    public CellStyleBuilder setCellBorder(int topColor,int leftColor,int bottomColor,int rightColor) {
        /**
         * 上边框及颜色
         */
        this.cellStyle.setBorderTop(BorderStyle.THIN);
        this.cellStyle.setTopBorderColor((short)topColor);
        /**
         * 左边框及颜色
         */
        this.cellStyle.setBorderLeft(BorderStyle.THIN);
        this.cellStyle.setLeftBorderColor((short)leftColor);

        /**
         * 下边框及颜色
         */
        this.cellStyle.setBorderBottom(BorderStyle.THIN);
        this.cellStyle.setBottomBorderColor((short)bottomColor);

        /**
         * 右边框及颜色
         */
        this.cellStyle.setBorderRight(BorderStyle.THIN);
        this.cellStyle.setRightBorderColor((short)rightColor);

        return this;
    }

    /**
     * 设置边框，每条边框对应一个颜色
     * 主要是用于设置物料列表的边框颜色
     *
     *
     * @return
     */
    public CellStyleBuilder setCellBorderForModule2Address() {
        /**
         * 上边框及颜色
         */
        this.cellStyle.setBorderTop(BorderStyle.THIN);
        this.cellStyle.setTopBorderColor((short)1);
        /**
         * 左边框及颜色
         */
        this.cellStyle.setBorderLeft(BorderStyle.THIN);
        this.cellStyle.setLeftBorderColor((short)1);

        /**
         * 下边框及颜色
         */
        this.cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        this.cellStyle.setBottomBorderColor((short)0);

        /**
         * 右边框及颜色
         */
        this.cellStyle.setBorderRight(BorderStyle.THIN);
        this.cellStyle.setRightBorderColor((short)0);

        return this;
    }

    /**
     * 设置边框，每条边框对应一个颜色
     * 主要是用于设置物料列表的边框颜色
     *
     *
     * @return
     */
    public CellStyleBuilder setCellBorderForModule2Limited() {
        /**
         * 上边框及颜色
         */
        this.cellStyle.setBorderTop(BorderStyle.THIN);
        this.cellStyle.setTopBorderColor((short)1);
        /**
         * 左边框及颜色
         */
        this.cellStyle.setBorderLeft(BorderStyle.THIN);
        this.cellStyle.setLeftBorderColor((short)1);

        /**
         * 下边框及颜色
         */
        this.cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        this.cellStyle.setBottomBorderColor((short)0);

        /**
         * 右边框及颜色
         */
        this.cellStyle.setBorderRight(BorderStyle.THIN);
        this.cellStyle.setRightBorderColor((short)1);

        return this;
    }



    /**
     * 创建并返回
     * @return
     */
    public CellStyle build() {
        return cellStyle;
    }

}
